/*============================================================================*/
/*                              x embedded systems                            */
/*============================================================================*/
/*                        OBJECT SPECIFICATION                                */
/*============================================================================*
* %name:            interrupts.c%
* %version:         1.0%
* %created_by:      David Robles%
* %date_created:    March 14 2012%
*=============================================================================*/
/* DESCRIPTION : Interrupts handling                                          */
/*============================================================================*/
/* FUNCTION COMMENT : Provides APIs to configure Interrupts                   */
/*                                                                            */
/*                                                                            */
/*============================================================================*/
/*                               OBJECT HISTORY                               */
/*============================================================================*/
/*  REVISION |   DATE      |                               |      AUTHOR      */
/*----------------------------------------------------------------------------*/
/*  1.0      | 14/03/2012  |                               | David Robles     */
/* File added from project software timers.                                   */
/*============================================================================*/

/* Includes */
/* -------- */
#include "stdtypedef.h"
#include "p18f4550.h"
#include "interrupts.h"
#include "hw_timers.h"


/* Functions macros, constants, types and datas         */
/* ---------------------------------------------------- */
/* Functions macros */

/*==================================================*/ 
/* Definition of constants                          */
/*==================================================*/ 
/* BYTE constants */

/* WORD constants */

/* LONG and STRUCTURE constants */


/*======================================================*/ 
/* Definition of RAM variables                          */
/*======================================================*/ 
/* BYTE RAM variables */

/* WORD RAM variables */

/* LONG and STRUCTURE RAM variables */

/*======================================================*/ 
/* close variable declaration sections                  */
/*======================================================*/ 

/* Private defines */

/* Private functions prototypes */
/* ---------------------------- */


/* Exported functions prototypes */
/* ----------------------------- */
void mw_interrupt_initCfg(void);
void mw_interrupt_Enable_Interrupts(void);
void mw_interrupt_Disable_Interrupts(void);

/* Inline functions */
/* ---------------- */
/**************************************************************
 *  Name                 : inline_func	2
 *  Description          :
 *  Parameters           :  [Input, Output, Input / output]
 *  Return               :
 *  Critical/explanation :    [yes / No]
 **************************************************************/
 
/* Private functions */
/* ----------------- */
/**************************************************************
 *  Name                 : private_func
 *  Description          :
 *  Parameters           :  [Input, Output, Input / output]
 *  Return               :
 *  Critical/explanation :    [yes / No]
 **************************************************************/

/* Exported functions */
/* ------------------ */
/**************************************************************
 *  Name                 : mw_interrupt_initCfg
 *  Description          : Initialize configuration for interrupts
 *  Parameters           :  none
 *  Return               :  none
 *  Critical/explanation :  No
 **************************************************************/
void mw_interrupt_initCfg(void)
{
	
	RCONbits.IPEN = 1;
/*  IPEN: Interrupt Priority Enable bit
    1 = Enable priority levels on interrupts
    0 = Disable priority levels on interrupts (PIC16CXXX Compatibility mode)
*/

    /********** CLEARING INTERRUPT FLAGS ************/

    INTCON3bits.INT1IF = 0;
/*  INT1IF: INT1 External Interrupt Flag bit
    1 = The INT1 external interrupt occurred (must be cleared in software)
    0 = The INT1 external interrupt did not occur
*/

    INTCON3bits.INT2IF = 0;
/*  INT2IF: INT2 External Interrupt Flag bit
    1 = The INT2 external interrupt occurred (must be cleared in software)
    0 = The INT2 external interrupt did not occur
*/

    INTCONbits.RBIF = 0;
/*  RBIF: RB Port Change Interrupt Flag bit
    1 = At least one of the RB7:RB4 pins changed state (must be cleared in software)
    0 = None of the RB7:RB4 pins have changed state
*/

    INTCONbits.INT0IF = 0;
/*  INT0IF: INT0 External Interrupt Flag bit
    1 = The INT0 external interrupt occurred (must be cleared in software)
    0 = The INT0 external interrupt did not occur
*/

    INTCONbits.TMR0IF = 0;
/*  TMR0IF: TMR0 Overflow Interrupt Flag bit
    1 = TMR0 register has overflowed (must be cleared in software)
    0 = TMR0 register did not overflow
*/

    PIR1bits.SSPIF = 0;
/*  SSPIF: Master Synchronous Serial Port Interrupt Flag bit
    1 = The transmission/reception is complete (must be cleared in software)
    0 = Waiting to transmit/receive
*/

   /********** ENABLING INTERRUPTS SECTION ************/

    INTCON3bits.INT1IE = 0;
/*  INT1IE: INT1 External Interrupt Enable bit
    1 = Enables the INT1 external interrupt
    0 = Disables the INT1 external interrupt
*/

    INTCON3bits.INT2IE = 0;
/*  INT2IE: INT2 External Interrupt Enable bit
    1 = Enables the INT2 external interrupt
    0 = Disables the INT2 external interrupt
*/

    INTCONbits.RBIE = 0;
/*  RBIE: RB Port Change Interrupt Enable bit
    1 = Enables the RB port change interrupt
    0 = Disables the RB port change interrupt
*/

    INTCONbits.INT0IE = 0;
/*  INT0IE: INT0 External Interrupt Enable bit
    1 = Enables the INT0 external interrupt
    0 = Disables the INT0 external interrupt
*/

    INTCONbits.TMR0IE = 1;
/*  TMR0IE: TMR0 Overflow Interrupt Enable bit
    1 = Enables the TMR0 overflow interrupt
    0 = Disables the TMR0 overflow interrupt
*/

    PIE1bits.SSPIE = 0;
/*  SSPIE: Master Synchronous Serial Port Interrupt Enable bit
    1 = Enables the MSSP interrupt
    0 = Disables the MSSP interrupt
*/

  /****** SETTING PRIORITIES *****/

    INTCON3bits.INT1IP = 0;
/*  INT1IP: INT1 External Interrupt Priority bit
    1 = High priority
    0 = Low priority
*/
    INTCON3bits.INT2IP = 0;
/*  INT2IP: INT2 External Interrupt Priority bit
    1 = High priority
    0 = Low priority
*/

    INTCON2bits.RBIP = 0;
/*  RBIP: RB Port Change Interrupt Priority bit
    1 = High priority
    0 = Low priority
*/

    INTCON2bits.TMR0IP = 1;
/*  TMR0IP: TMR0 Overflow Interrupt Priority bit
    1 = High priority
    0 = Low priority
*/

   IPR1bits.SSPIP = 0;
/* SSPIP: Master Synchronous Serial Port Interrupt Priority bit
   1 = High priority
   0 = Low priority
*/

    /***** EXTERNAL INTERRUPTS CONFIGURATION ******/
    
    INTCON2bits.INTEDG2 = 0;
/*  INTEDG2: External Interrupt 2 Edge Select bit
    1 = Interrupt on rising edge
    0 = Interrupt on falling edge
*/

    INTCON2bits.INTEDG1 = 0;
/*  INTEDG1: External Interrupt 1 Edge Select bit
    1 = Interrupt on rising edge
    0 = Interrupt on falling edge
*/

    INTCON2bits.INTEDG0 = 0;
/*  INTEDG0: External Interrupt 0 Edge Select bit
    1 = Interrupt on rising edge
    0 = Interrupt on falling edge
*/

    /***** OTHERS CONFIGURATIONS ****/ 
    INTCON2bits.NOT_RBPU = 0;
/*  RBPU: PORTB Pull-up Enable bit
    1 = All PORTB pull-ups are disabled
    0 = PORTB pull-ups are enabled by individual port latch values
*/

    INTCONbits.PEIE = 0;
/*  PEIE/GIEL: Peripheral Interrupt Enable bit
    When IPEN = 0:
    1 = Enables all unmasked peripheral interrupts
    0 = Disables all peripheral interrupts
    When IPEN = 1:
    1 = Enables all low priority peripheral interrupts
    0 = Disables all low priority peripheral interrupts
*/


    mw_interrupt_Enable_Interrupts();
}

/**************************************************************
 *  Name                 : mw_interrupt_Enable_Interrupts
 *  Description          : Enable interrupts
 *  Parameters           :  none
 *  Return               :  none
 *  Critical/explanation :  No
 **************************************************************/

void mw_interrupt_Enable_Interrupts(void)
{
    INTCONbits.GIE = 1;
/*	GIE/GIEH: Global Interrupt Enable bit
    When IPEN = 0:
    1 = Enables all unmasked interrupts
    0 = Disables all interrupts
    When IPEN = 1:
    1 = Enables all high priority interrupts
    0 = Disables all high priority interrupts
*/
}

/**************************************************************
 *  Name                 : mw_interrupt_Disable_Interrupts
 *  Description          : Disable interrupts
 *  Parameters           :  none
 *  Return               :  none
 *  Critical/explanation :  No
 **************************************************************/

void mw_interrupt_Disable_Interrupts(void)
{
    INTCONbits.GIE = 0;
/*	GIE/GIEH: Global Interrupt Enable bit
    When IPEN = 0:
    1 = Enables all unmasked interrupts
    0 = Disables all interrupts
    When IPEN = 1:
    1 = Enables all high priority interrupts
    0 = Disables all high priority interrupts
*/
}

